package com.sms.activity;

import java.util.ArrayList;
import java.util.HashMap;

import com.sms.database.datastore;
import com.sms.manager.MessageListAdapter;
import com.sms.model.SMS;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Deletemessages extends ListActivity{
	
	public Context context=this;
	private datastore db;
	Cursor cursor = null;
	//private ArrayList<HashMap<String,String>> msg;
	private ArrayList<SMS> msg;
	//private String[] msg;
	ListView lv;
	public void onCreate(Bundle savedInstanceState) {
        setTitle("Delete Message");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sentlist);
        lv= getListView();
		db =new datastore(this);
		refresh();
		lv.setOnItemClickListener(new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
		{
		    	final int pos=position;
		    	AlertDialog.Builder builder = new AlertDialog.Builder(Deletemessages.this);
		    	builder.setMessage("Are you sure you want to delete the Message?").setCancelable(false)
		    	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	           public void onClick(DialogInterface dialog, int id) {
		    	                
		    	        	   datastore db = new datastore(Deletemessages.this);
		    	        	   db.openDB();
		    	        	   db.remove(msg.get(pos).getId());
		    	        	   db.close();
		    	        	   refresh();
		    	        	   
		    	           }
		    	       })
		    	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		    	           public void onClick(DialogInterface dialog, int id) {
		    	                dialog.cancel();
		    	           }
		    	       });
		    	AlertDialog alert = builder.create();
		    	alert.show();
		    	
		      // When clicked, show a toast with the TextView text
		      Toast.makeText(getApplicationContext(), msg.get(position).getPhoneNumber(),Toast.LENGTH_SHORT).show();
		    }
		  });
		}
	public void refresh()
	{
		db.openDB();
		msg = db.fetchAllMessages();
		Log.d("size", msg.size()+"");
		lv.setAdapter(new MessageListAdapter(getApplicationContext(),msg));
		db.close();
	}

}
